package com.gitee.fsmxm.ocon.util;

import com.gitee.fsmxm.ocon.constant.AppEnum;
import com.gitee.fsmxm.ocon.core.UnsupportedAppException;

import java.util.HashMap;
import java.util.Map;

import static com.gitee.fsmxm.ocon.constant.CVTC.*;

/**
 * @author fsmxm
 */
public class AppUtils {

    private AppUtils() {
    }

    /**
     * app name -> win app name
     */
    private static final Map<String, String> APP_MAPPINGS;
    /**
     * app name -> win exe name
     */
    private static final Map<String, String> IMAGE_MAPPINGS;
    /**
     * app name -> dispatches
     */
    private static final Map<String, String> DISPATCH_MAPPINGS;

    /**
     * ext name -> app enum
     */
    private static final Map<String, AppEnum> SUPPORT_APP_MAPPINGS;

    static {
        APP_MAPPINGS = new HashMap<>(6);
        APP_MAPPINGS.put("word.ms", "Word.Application");
        APP_MAPPINGS.put("excel.ms", "Excel.Application");
        APP_MAPPINGS.put("powerpoint.ms", "Powerpoint.Application");
        APP_MAPPINGS.put("word.wps", "Kwps.Application");
        APP_MAPPINGS.put("excel.wps", "Ket.Application");
        APP_MAPPINGS.put("powerpoint.wps", "Kwpp.Application");

        IMAGE_MAPPINGS = new HashMap<>(6);
        IMAGE_MAPPINGS.put("word.ms", "winword.exe");
        IMAGE_MAPPINGS.put("excel.ms", "excel.exe");
        IMAGE_MAPPINGS.put("powerpoint.ms", "powerpnt.exe");
        IMAGE_MAPPINGS.put("word.wps", "wps.exe");
        IMAGE_MAPPINGS.put("excel.wps", "et.exe");
        IMAGE_MAPPINGS.put("powerpoint.wps", "wpp.exe");

        DISPATCH_MAPPINGS = new HashMap<>(6);
        DISPATCH_MAPPINGS.put("word.ms", "Documents");
        DISPATCH_MAPPINGS.put("excel.ms", "Workbooks");
        DISPATCH_MAPPINGS.put("powerpoint.ms", "Presentations");
        DISPATCH_MAPPINGS.put("word.wps", "Documents");
        DISPATCH_MAPPINGS.put("excel.wps", "Workbooks");
        DISPATCH_MAPPINGS.put("powerpoint.wps", "Presentations");

        SUPPORT_APP_MAPPINGS = new HashMap<>(6);
        SUPPORT_APP_MAPPINGS.put(DOC, AppEnum.WORD);
        SUPPORT_APP_MAPPINGS.put(DOCX, AppEnum.WORD);
        SUPPORT_APP_MAPPINGS.put(PPT, AppEnum.POWERPOINT);
        SUPPORT_APP_MAPPINGS.put(PPTX, AppEnum.POWERPOINT);
        SUPPORT_APP_MAPPINGS.put(XLS, AppEnum.EXCEL);
        SUPPORT_APP_MAPPINGS.put(XLSX, AppEnum.EXCEL);
    }

    public static String checkAndGetApplication(String appName) throws UnsupportedAppException {
        String application = APP_MAPPINGS.get(appName.toLowerCase());
        if (application == null) {
            throw new UnsupportedAppException("application [" + appName + "] not support.");
        }
        return application;
    }

    public static String getImage(String appName) {
        return IMAGE_MAPPINGS.get(appName.toLowerCase());
    }

    public static String getImage(String appName, String val) {
        if (val != null && !val.isEmpty()) {
            return val;
        }
        return getImage(appName);
    }

    public static String getDispatch(String appName) {
        return DISPATCH_MAPPINGS.get(appName.toLowerCase());
    }

    public static AppEnum getSupportApp(String ext) {
        return SUPPORT_APP_MAPPINGS.get(ext.toLowerCase());
    }
}
